package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.AlbumAddNewParam;
import cn.tedu.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/albums")
@Api(tags = "04.相册管理模块")
@Slf4j
public class AlbumController {

    @Autowired
    private IAlbumService albumService;

    // http://localhost:9480/albums/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)

    public JsonResult addNew(@Valid AlbumAddNewParam albumAddNewParam) {
        log.debug("开始处理[添加相册]的请求，参数{}", albumAddNewParam);
        albumService.addNew(albumAddNewParam);

        return JsonResult.ok();
    }


    // http://localhost:9480/albums/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据Id删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")

    })
    //检查未封装的参数       ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    //不修改@RequestParam会报id异常错误，不添加API文档会不显示，所以要添加@PathVariable
    public JsonResult delete(@Range(min = 1, message = "根据ID删除相册失败，请提交合法的Id值！")
                                 @PathVariable Long id) {

        log.debug("开始处理[删除相册]的请求，参数{}", id);
        albumService.delete(id);
        return JsonResult.ok();

    }

    // http://localhost:9480/albums/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改相册详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的相册ID值！") Long id,
                                     @Valid AlbumUpdateInfoParam albumUpdateInfoParam) {
        log.debug("开始处理【修改相册详情】的业务，参数：{}", albumUpdateInfoParam);
        albumService.updateInfoById(id, albumUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9480/albums
    @GetMapping("")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(
            @Range(min = 1, message = "查询相册列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<AlbumListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = albumService.list(pageNum, Integer.MAX_VALUE);
        }else{
            pageData = albumService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

    // http://localhost:9480/albums/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")

    })

    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询相册详情失败相册失败，请提交合法的Id值！")
                                      @PathVariable Long id) {

        log.debug("开始处理[根据ID查询相册]的请求，参数{}", id);
        AlbumStandardVO result = albumService.getStandardById(id);
        return JsonResult.ok(result);

    }
}
